library(tidyverse)
library(readxl)
get_list = . %>% unlist() %>% as.integer() %>% na.omit()
path = "Excel/800-899/822/822 Largest N Digit Numbers in Grids.xlsx"
input1 = read_excel(path, range = "A3:C5", col_names = FALSE) %>% as.matrix()
test1 = read_excel(path, range = "J3:M3", col_names = FALSE) %>% get_list()
input2 = read_excel(path, range = "A7:D10", col_names = FALSE) %>% as.matrix()
test2 = read_excel(path, range = "J7:M7", col_names = FALSE) %>% get_list()
input3 = read_excel(path, range = "A12:E16", col_names = FALSE) %>% as.matrix()
test3 = read_excel(path, range = "J12:M12", col_names = FALSE) %>% get_list()
roll_max_num <- function(v, n) {
if (length(v) < n) return(-Inf)
map_int(1:(length(v) - n + 1), ~ as.integer(paste0(v[.x:(.x + n - 1)], collapse = ""))) %>% max()
}
max_for_N <- function(mat, n) {
rows <- asplit(mat, 1) %>% map_int(roll_max_num, n)
cols <- asplit(mat, 2) %>% map_int(roll_max_num, n)
max(c(rows, cols))
}
result1 = map_int(2:nrow(input1), ~ max_for_N(input1, .x))
result2 = map_int(2:nrow(input2), ~ max_for_N(input2, .x))
result3 = map_int(2:nrow(input3), ~ max_for_N(input3, .x))
all.equal(result1, test1, check.attributes = FALSE) # one different than provided
all.equal(result2, test2, check.attributes = FALSE) # all correct
all.equal(result3, test3, check.attributes = FALSE) # two different than providedExcel BI - Excel Challenge 822
excel-challenges
excel-formulas
🔰 Find the largest N digit numbers in square grids considering rows (left to right) and columns (top to bottom).

Challenge Description
🔰 Find the largest N digit numbers in square grids considering rows (left to right) and columns (top to bottom).
Solutions
- Logic: Read the workbook ranges needed for the challenge.
- Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
- Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
- Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
import pandas as pd
path = "800-899/822/822 Largest N Digit Numbers in Grids.xlsx"
input1 = pd.read_excel(path, header=None, usecols="A:C", skiprows=2, nrows=3).values
test1 = pd.read_excel(path, header=None, usecols="J:M", skiprows=2, nrows=1).values.flatten(); test1 = [x for x in test1 if pd.notna(x)]
input2 = pd.read_excel(path, header=None, usecols="A:D", skiprows=6, nrows=4).values
test2 = pd.read_excel(path, header=None, usecols="J:M", skiprows=6, nrows=1).values.flatten(); test2 = [x for x in test2 if pd.notna(x)]
input3 = pd.read_excel(path, header=None, usecols="A:E", skiprows=11, nrows=5).values
test3 = pd.read_excel(path, header=None, usecols="J:M", skiprows=11, nrows=1).values.flatten(); test3 = [x for x in test3 if pd.notna(x)]
def roll_max_num(v, n):
v = [str(x) for x in v]
if len(v) < n:
return float('-inf')
nums = [int(''.join(v[i:i+n])) for i in range(len(v)-n+1)]
return max(nums)
def max_for_N(mat, n):
rows = [roll_max_num(row, n) for row in mat]
cols = [roll_max_num(col, n) for col in mat.T]
return max(rows + cols)
result1 = [max_for_N(input1, n) for n in range(2, input1.shape[0]+1)]
result2 = [max_for_N(input2, n) for n in range(2, input2.shape[0]+1)]
result3 = [max_for_N(input3, n) for n in range(2, input3.shape[0]+1)]
print(result1 == test1) # one different than provided
print(result2 == test2) # all correct
print(result3 == test3) # two different than providedThe Python version keeps the algorithm explicit, which helps when the challenge depends on a greedy or iterative rule.
Difficulty Level
Easy / Medium
The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.